package com.haochang.chunkvod.controller.activity.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.order.OrderData;
import com.haochang.chunkvod.model.order.RoomInfo;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

/**
 * 点餐--预结查询
 *     @author Yangjie
 *     className ExpectedQueryListActivity
 *     created at  2016/11/18  16:08
 */
public class ExpectedQueryListActivity extends BaseActivity
{
	private TitleView titleView;
	private BaseListView listView;
	private OrderData orderData;
	private QuickAdapter<RoomInfo> adapter;
	private List<RoomInfo> orderInfoList = new ArrayList<RoomInfo>();
	private int keyOfItemTag = R.id.clicked_item;
	private int keyOfPositionTag = R.id.item_position;

	@Override
	public void receiveParam()
	{
	}

	@Override
	public void initData()
	{
		orderData = new OrderData(ExpectedQueryListActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_expected_query_list);
		titleView = (TitleView) findViewById(R.id.titleView);
		listView = (BaseListView) findViewById(R.id.listView);
		titleView.setTitle(R.string.expected_query).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(ExpectedQueryListActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
	}

	@Override
	public void initViewsData()
	{
		super.initViewsData();
		adapter = new QuickAdapter<RoomInfo>(this, R.layout.item_expected_query, orderInfoList)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, RoomInfo item)
			{
				if (null != item)
				{
					helper.setText(R.id.tv_room, item.getRoomNum());
					helper.setText(R.id.tv_time, TimeUtils.misToHour2(item.getRest()));
					helper.getView(R.id.bt_remove).setTag(keyOfPositionTag, helper.getPosition());
					helper.getView(R.id.bt_remove).setTag(keyOfItemTag, item);
					helper.getView(R.id.bt_remove).setOnClickListener(onBaseClickListener);
				}
			}
		};
		listView.setAdapter(adapter);
		requestExpectedList(true);
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{//显示弹出框
			RoomInfo item = (RoomInfo) v.getTag(keyOfItemTag);
			Integer positon = (Integer) v.getTag(keyOfPositionTag);
			if (item != null && positon != null)
			{
				requestDeleteExpectedItem(positon, item.getRoomId());
			} else
			{
				ToastUtils.showText(ExpectedQueryListActivity.this, "删除预结房间异常!");
			}
		}
	};

	/**
	 * 获取预结列表
	 */
	@SuppressWarnings("unchecked")
	private void requestExpectedList(boolean showLoadingAnimation)
	{
		orderData.requestExpectedList(showLoadingAnimation, new OrderData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					orderInfoList.clear();
					orderInfoList = list;
					adapter.replaceAll(orderInfoList);
				} else
				{
					ToastUtils.showText(ExpectedQueryListActivity.this, "预结列表为空！");
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				ToastUtils.showText(ExpectedQueryListActivity.this, TextUtils.isEmpty(errMsg) ? "获取预收列表失败!" : errMsg);
			}
		});
	}

	/**
	 * 删除预结房间
	 * @param position 删除item项的位置
	 */
	private void requestDeleteExpectedItem(final int position, String roomId)
	{
		orderData.requestDeleteExpectedItem(roomId, new OrderData.ResultResponse()
		{
			@Override
			public void onRequestSuccess()
			{
				ToastUtils.showText(ExpectedQueryListActivity.this, "删除预结房间成功!");
				orderInfoList.remove(position);
				adapter.replaceAll(orderInfoList);
			}

			@Override
			public void onFail(int errorNo, String errorMsg)
			{
				ToastUtils.showText(ExpectedQueryListActivity.this, TextUtils.isEmpty(errorMsg) ? "删除预结房间失败!" : errorMsg);
			}
		});
	}
}
